import ExcavationOrigin from "./excavationOrigin";
import CreateRegularPolygon from "../../Draw/Polygon/CreatePolygon";

const Cesium = window.Cesium;
export default class Excavation {
  _viewer: any;
  _excavationEntity: Array<any>;
  constructor(viewer) {
    this._excavationEntity = [];
    this._viewer = viewer;
  }
  get excavationEntity(){
    return this._excavationEntity
  }
  create(callbackForResult, option) {

    let viewer = this._viewer;
    if (!viewer.terrainProvider._layers) {
      if (callbackForResult && typeof callbackForResult == "function")
        callbackForResult("请先开启地形");
      return;
    }
    option.height = option.height || 1;
    option.precision = option.precision || 256;
    var result = {
      allArea: 0,
      cutArea: 0,
      cutVolume: 0,
      fillArea: 0,
      fillVolume: 0,
      noArea: 0,
    };
    var canvas = viewer.scene.canvas;
    let handler = new Cesium.ScreenSpaceEventHandler(canvas);
    var polygon;
    const $this = this;
    const callback = (cp) => {
      const analysisObj = new ExcavationOrigin(
        viewer,
        cp.PottingPoint.positions,
        option.height,
        option.precision,
        (e) => {
          polygon = e;
        }
      );
      result = analysisObj.VolumeAnalysis((e) => {
        $this._excavationEntity.push(e);
        $this._excavationEntity.forEach((item) => {});
      });
      viewer.entities.remove(cp);
      callbackForResult(result);
    };
    CreateRegularPolygon(
      viewer,
      handler,
      [],
      {
        onground: false,
      },
      callback
    );
  }
  removeAll() {
    let viewer = this._viewer;
    this._excavationEntity.forEach((item) => {
      item.forEach((id) => {
        viewer.entities.removeById(id);
      });
    });
  }
}
